/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.dialogs;

import org.eclipse.swt.SWT;  
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.mags.remas.model.commons.DataSourceInformation;
import org.mags.remas.view.commons.ImageProvider;

/**
 * 
 * @author Mario Gaitan
 *
 */
public abstract class AbstractSolicitorDialog extends AbstractDialog {
	protected abstract void createContent(Composite parent);
	public abstract void setInformation2Object(DataSourceInformation dataSourceInformation);
	public abstract void setInformation2Controls(DataSourceInformation dataSourceInformation);
	public abstract boolean equalsDriverName(String driverName);
	public abstract String getId();
	public abstract void clear();
	public abstract boolean validate();
	public boolean accept = false; 
	@Override
	public void show() {
		accept = false;
		super.show();
	}
    protected GridData getCellLayoutData() {
		GridData cellLayoutData = new GridData();
		cellLayoutData.horizontalAlignment = SWT.FILL;
		cellLayoutData.grabExcessHorizontalSpace = true;
		return cellLayoutData;
    }    	
	protected Label createLabel(Composite parent, String text, int span) {
		Label label = new Label(parent, SWT.NONE);
		label.setText(text);
		GridData gridData = getCellLayoutData();
		gridData.horizontalSpan = span;
		label.setLayoutData(gridData);
		return label;
	}
	protected Text createPassword(Composite parent, int span, int textLimit) {
		Text text = new Text(parent, SWT.BORDER|SWT.PASSWORD);
		text.setText("");
		text.setTextLimit(textLimit);
		GridData gridData = getCellLayoutData();
		gridData.horizontalSpan = span;
		text.setLayoutData(gridData);
		return text;
	}		
	protected Text createText(Composite parent, int span, int textLimit) {
		Text text = new Text(parent, SWT.BORDER);
		text.setText("");
		text.setTextLimit(textLimit);
		GridData gridData = getCellLayoutData();
		gridData.horizontalSpan = span;
		text.setLayoutData(gridData);
		return text;
	}	
	protected Button createButton(Composite parent, String text, int span) {
		Button button = new Button(parent, SWT.NONE);
		button.setText(text);
		GridData gridData = getCellLayoutData();
		gridData.horizontalSpan = span;
		button.setLayoutData(gridData);
		return button;
	}	
	public AbstractSolicitorDialog(Display display) {
		super(display, SWT.NONE);
	}
	@Override
	protected void createPartControl(Composite parent) {
		GridLayout gridLayout = new GridLayout(1, true);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		parent.setLayout(gridLayout);
		createContent(parent);
		createButtons(parent);				
	}
	public boolean isAccept() {
		return accept;
	}
	@Override
	public void cancel() {
		hide();
	}	
	@Override
	public void accept() {
		if(validate()) {
			hide();
			accept = true;
		}
	}
    protected void createButtons(Composite parent) {
    	Composite buttons = new Composite(parent, SWT.NONE);
    	buttons.setLayout(new GridLayout(2, true));
		GridData cellLayoutData = new GridData();
		cellLayoutData.horizontalAlignment = SWT.RIGHT;
		cellLayoutData.grabExcessHorizontalSpace = true;
    	buttons.setLayoutData(cellLayoutData);
    	
        Button cancelButton = new Button(buttons, SWT.NONE);
		cancelButton.setText("cancel");
		cancelButton.setImage(ImageProvider.getCommonImage("cancel.png"));
		cellLayoutData = new GridData();
		cellLayoutData.horizontalAlignment = SWT.FILL;
		cellLayoutData.grabExcessHorizontalSpace = true;		
		cancelButton.setLayoutData(cellLayoutData);
		cancelButton.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				cancel();
			}
		});
		
		Button acceptButton = new Button(buttons, SWT.NONE);
		acceptButton.setText("accept");
		acceptButton.setImage(ImageProvider.getCommonImage("accept.png"));
		cellLayoutData = new GridData();
		cellLayoutData.horizontalAlignment = SWT.FILL;
		cellLayoutData.grabExcessHorizontalSpace = true;				
		acceptButton.setLayoutData(cellLayoutData);		
		acceptButton.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				accept();
			}
		});    	
    }	
}
